<?php
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/5/22
 * Time: 17:11
 */

namespace Hilaoyu\LaravelExtend\Providers;

use Hilaoyu\Utils\UtilStr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Request;
class RequestMacroServiceProvider extends ServiceProvider
{
    public function boot()
    {


    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        request()->offsetUnset('/' . request()->path());
        request()->query->remove('/' . request()->path());


        Request::macro('getClientPlatform',function (){
            $platform = request()->header(
                'ClientPlatform'
                ,request()->header(
                    'AuthPlatform'
                    ,request()->input('ClientPlatform',request()->input(
                        'AuthPlatform'
                        ,'')
                    )
                )
            );
            if(empty($platform)){
                $session = request()->session();
                if($session){
                    $platform = $session->get('login_client_platform','');
                }

            }
            if(empty($platform)){
                //$user_agent = request()->header('user-agent','');
                $platform = 'web';
            }
            return $platform;
        });
        Request::macro('getClientSource',function (){
            $source = request()->header(
                'ClientSource'
                ,request()->header(
                    'AuthSource'
                    ,request()->input(
                        'ClientSource'
                        ,request()->input('AuthSource','')
                    )
                )
            );

            empty($source) && $this->hasSession() && ($source = $this->session()->get('login_client_source'));
            empty($source) && ($source = request()->ip());

            return $source;
        });

        Request::macro('isMobile',function (){
            //如果是在微信中 则返回 true
            if(request()->isWechat()){
                return true;
            }


            if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
                return true;
            } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
                return true;
            } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
                return true;
            } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
                return true;
            } else {
                return false;
            }
        });
        Request::macro('isWechat',function (){
            if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
                return true;
            } else {
                return false;
            }
        });
        Request::macro('isApp',function (){
            return 'app' ==UtilStr::lower(request()->getClientPlatform());

        });
        Request::macro('isWeb',function (){
            return !request()->isWechat() && !request()->isApp();

        });

        Request::macro('isFromOwn', function () {
            return Str::startsWith(request()->header('referer'),request()->getSchemeAndHttpHost());
        });
    }
}
